package com.d3works.apps.businessproject.db.repo;

import com.d3works.apps.base.db.AppBusinessProject;
import com.d3works.apps.base.db.repo.AppBusinessProjectRepository;
import com.d3works.apps.businessproject.exception.BusinessProjectExceptionMessage;
import com.d3works.apps.project.db.repo.ProjectTaskRepository;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import javax.persistence.PersistenceException;

public class AppBusinessProjectManagementRepository extends AppBusinessProjectRepository {

  @Inject private ProjectTaskRepository projectTaskRepo;

  @Override
  public AppBusinessProject save(AppBusinessProject entity) {
    try {
      if (!Strings.isNullOrEmpty(entity.getExculdeTaskInvoicing())) {
        projectTaskRepo.all().filter(entity.getExculdeTaskInvoicing()).count();
      }
      return super.save(entity);
    } catch (Exception e) {
      TraceBackService.traceExceptionFromSaveMethod(e);
      throw new PersistenceException(
          I18n.get(BusinessProjectExceptionMessage.INVALID_EXCLUDE_TASK_FILTER));
    }
  }
}
